/******************************************************************************
 * Author:      671643387
 * Created:     2016/9/29
 *****************************************************************************/

/**
 * 生成扑克牌
 * @param category
 * @returns {Array}
 */
function genLaomazi(category) {
    var laomazis = [];

    var categoryConf = gConfLaomazi[category];

    for (var type in categoryConf) {
        if (!categoryConf.hasOwnProperty(type)) {
            continue;
        }

        type = +type;
        var typeConf = categoryConf[type];
        for (var val in typeConf) {
            if (!typeConf.hasOwnProperty(val)) {
                continue;
            }

            val = +val;

            if ((type == 2 && val != 1) || (type == 1 && val == 2)) {
                continue;
            }
            laomazis.push({
                type: type,  //字面值
                value: val   //花色
            });
        }
    }

    var randomLaomazis = [];
    while (laomazis.length > 0) {
        var idx = Math.floor(Math.random() * laomazis.length);
        randomLaomazis.push(laomazis[idx]);
        laomazis.splice(idx, 1);
    }

    return randomLaomazis;
}

/**
 * 排序手牌
 * @param laomazis
 * @returns {Array}
 */
function sortCards(laomazis) {
    var sortedArrs = {};
    var newLaomazis = [];
    var types = {};
    // 统计类型
    laomazis.forEach(function(laomazi){
        types[laomazi.type] = 1;
    });

    for (var type in types) {
        if (!types.hasOwnProperty(type)) {
            continue;
        }

        type = +type;
        var values = [];
        laomazis.forEach(function (laomazi) {
            if (laomazi.type == type) {
                values.push(+laomazi.value);
            }
        });
        values.sort();

        values.forEach(function (val) {
            newLaomazis.push({
                type: type,
                value: val,
            });
        });
    }
    return newLaomazis;
}

exports.genLaomazi = genLaomazi;
exports.sortCards = sortCards;